package com.kll.tv.views.ui

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

class CircleBarView: View {
    lateinit var progressPaint: Paint
    lateinit var tatalProgressPaint: Paint
    var maxValue:Int = 100
        set(value) {
            field = value
            invalidate()
        }
    var currentValue:Int = 0
        set(value) {
            field = value
            invalidate()
        }

    constructor(context: Context?): this(context, null){

    }

    constructor(context: Context?, attrs: AttributeSet?): this(context, attrs, 0){

        init(context!!, attrs)
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr){

    }


    @SuppressLint("ResourceType")
    private fun init(context: Context, attrs: AttributeSet? ){
//        rPaint = Paint()
//        rPaint.setStyle(Paint.Style.STROKE);//只描边，不填充
//        rPaint.setColor(Color.RED);

        progressPaint = Paint()
        progressPaint.style = Paint.Style.STROKE //只描边，不填充
        progressPaint.strokeWidth = 20f
        progressPaint.color = Color.parseColor("#CCCCCC")
        progressPaint.setAntiAlias(true) //设置抗锯齿

//        tatalProgressPaint = Paint()
//        tatalProgressPaint.style = Paint.Style.STROKE
//        tatalProgressPaint.strokeWidth = context.resources.getDimension(R.dimen.x8)
//        tatalProgressPaint.color = Color.TRANSPARENT
//        tatalProgressPaint.setAntiAlias(true)
    }

    @SuppressLint("ResourceType")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas);
        var x:Float = 10f
        var y:Float = 10f
        var rectF = RectF(x, y, measuredWidth.toFloat() - x, measuredWidth.toFloat() - y);//建一个大小为300 * 300的正方形区域

        // 计算反向值
        var tempValue = maxValue - currentValue
        tempValue = if(tempValue > maxValue) maxValue else if(tempValue < 0) 0 else tempValue

        var sweepAngle = tempValue.toFloat() * 360 / maxValue.toFloat()

        //MyLog.v(">>>angle",sweepAngle.toString());

        //canvas.drawArc(rectF, 0f, 360f, false, tatalProgressPaint)
        canvas.drawArc(rectF,-90f,-sweepAngle,false,progressPaint);//这里角度0对应的是三点钟方向，顺时针方向递增
//        canvas.drawRect(rectF,rPaint);

    }
}